package com.infoauto.opc.gateway.controller;

import com.infoauto.opc.gateway.entity.Result;
import com.infoauto.opc.gateway.entity.ResultCode;
import com.infoauto.opc.gateway.model.gtm4.CheckTheNumberOfUnits1;
import com.infoauto.opc.gateway.model.gtm4.CheckTheProcess;
import com.infoauto.opc.gateway.model.gtm4.EquipmentAbnormal;
import com.infoauto.opc.gateway.model.gtm4.EquipmentFailure;
import com.infoauto.opc.gateway.service.EquipmentAbnormalService;
import com.infoauto.opc.gateway.service.EquipmentFailureService;
import com.sun.deploy.net.URLEncoder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 设备故障
 */

@CrossOrigin
@RestController
@RequestMapping("/equipmentFailure")
public class EquipmentFailureController {
    @Autowired
    private EquipmentFailureService service;

    @RequestMapping(value="/list" ,method= RequestMethod.GET)
    public Result selectByProducTionTimeAndShift(String productionTime,Integer shift){
        try{
            List<EquipmentFailure> list = service.selectByProducTionTimeAndShift(productionTime,shift);
            return new Result(ResultCode.SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;

   }
    /**
     * 修改设备故障
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Result update(@RequestBody EquipmentFailure equipmentFailure) {
        //2.调用service更新
        service.update(equipmentFailure);
        return new Result(ResultCode.SUCCESS);
    }
    @RequestMapping(value = "/updateRemark", method = RequestMethod.PUT)
    public Result updateRemark(String id,String remark) {
        EquipmentFailure equipmentFailure = service.selectById(id);
        if(equipmentFailure!=null){
            equipmentFailure.setRemark(remark);
            equipmentFailure.setModifyDateTime(new Date());
            //2.调用service更新
            service.update(equipmentFailure);
        }
        return new Result(ResultCode.SUCCESS);
    }

    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public void export(HttpServletResponse response, String productionTime, Integer shift) throws Exception {
        //1.构造数据
        List<EquipmentFailure> list = service.selectByProducTionTimeAndShift(productionTime,shift);
        //2.创建工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();
        String[] head1 = {"GTMC#4"};
        //3.构造sheet
        Sheet sheet = workbook.createSheet("设备故障");
        //创建单元格样式对象
        CellStyle cellStyle = workbook.createCellStyle();
        //设置居中显示
        cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        cellStyle.setBorderBottom(BorderStyle.MEDIUM);//下边框
        cellStyle.setBorderTop(BorderStyle.MEDIUM);//上边框
        cellStyle.setBorderLeft(BorderStyle.MEDIUM);//左边框
        cellStyle.setBorderRight(BorderStyle.MEDIUM);//有边框
        //设置单元格样式
        //设置边框
        //创建单元格样式对象
        CellStyle cellStyle1 = workbook.createCellStyle();
        cellStyle1.setBorderBottom(BorderStyle.NONE);//下边框
        cellStyle1.setBorderTop(BorderStyle.NONE);//上边框
        //设置字体
        Font font = workbook.createFont();//创建字体对象
        font.setFontName("宋体");//设置字体
        font.setFontHeightInPoints((short)16);//设置字号
        cellStyle1.setFont(font);
        Row row =sheet.createRow(0);
        AtomicInteger headersAi = new AtomicInteger();
        for (String title : head1) {
            Cell cell = row.createCell(headersAi.getAndIncrement());
            cell.setCellValue(title);
            cell.setCellStyle(cellStyle1);
        }
        //合并单元格
        CellRangeAddress region1 =new CellRangeAddress(0, 0, 0, 11);
        sheet.addMergedRegion(region1);
        AtomicInteger ai1 = new AtomicInteger(1);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Cell cell = null;
        Row row2 = sheet.createRow(ai1.getAndIncrement());
        cell = row2.createCell(0);
        cell.setCellValue("Date");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row2.createCell(1);
        cell.setCellValue(productionTime);
        //设置样式
        cell.setCellStyle(cellStyle);
        sheet.setColumnWidth(1, 12 * 256);//设置第一列的宽度是31个字符宽度
        sheet.setColumnWidth(2, 12 * 256);//设置第一列的宽度是31个字符宽度
        sheet.setColumnWidth(3, 12 * 256);//设置第一列的宽度是31个字符宽度
        sheet.setColumnWidth(4, 12 * 256);//设置第一列的宽度是31个字符宽度
        sheet.setColumnWidth(5, 12 * 256);//设置第一列的宽度是31个字符宽度
        sheet.setColumnWidth(6, 12 * 256);//设置第一列的宽度是31个字符宽度
        sheet.setColumnWidth(7, 12 * 256);//设置第一列的宽度是31个字符宽度
        sheet.setColumnWidth(8, 12 * 256);//设置第一列的宽度是31个字符宽度
        sheet.setColumnWidth(9, 12 * 256);//设置第一列的宽度是31个字符宽度
        sheet.setColumnWidth(10, 12 * 256);//设置第一列的宽度是31个字符宽度
        sheet.setColumnWidth(11, 30 * 256);//设置第一列的宽度是31个字符宽度
        row2.setHeightInPoints(22);//设置行的高度是50个点
        AtomicInteger ai2 = new AtomicInteger(2);
        Row row3 = sheet.createRow(ai2.getAndIncrement());
        cell = row3.createCell(0);
        cell.setCellValue("Shift");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row3.createCell(1);
        cell.setCellValue(shift);
        //设置样式
        cell.setCellStyle(cellStyle);
        //合并单元格
        CellRangeAddress region2 =new CellRangeAddress(1, 2, 2, 11);
        sheet.addMergedRegion(region2);
        // 合并单元格上边框样式
//        AtomicInteger ai3 = new AtomicInteger(3);
//        Row row4 = sheet.createRow(ai3.getAndIncrement());
//        cell = row4.createCell(0);
//        cell.setCellValue("");
//        //设置样式
//        cell.setCellStyle(cellStyle);
//
//        cell = row4.createCell(2);
//        cell.setCellValue("检查通过台数1");
//        //设置样式
//        cell.setCellStyle(cellStyle);
//        CellRangeAddress region3 =new CellRangeAddress(3, 3, 0, 1);
//        //合并单元格
//        sheet.addMergedRegion(region3);
//        CellRangeAddress region4 =new CellRangeAddress(3, 3, 2, 12);
//        //合并单元格
//        sheet.addMergedRegion(region4);

        AtomicInteger ai3 = new AtomicInteger(3);
        Row row4 = sheet.createRow(ai3.getAndIncrement());
        cell = row4.createCell(0);
        cell.setCellValue("编号");
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(1);
        cell.setCellStyle(cellStyle);
        cell.setCellValue("时间带");
        cell = row4.createCell(2);
        cell.setCellValue("中涂工序");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(3);
        cell.setCellValue("色漆1工序");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(4);
        cell.setCellValue("色漆2工序");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(5);
        cell.setCellValue("色差工序");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(6);
        cell.setCellValue("清漆工序");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(7);
        cell.setCellValue("检查工序");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(8);
        cell.setCellValue("上挂工序");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(9);
        cell.setCellValue("吹气工序");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(10);
        cell.setCellValue("干燥炉");
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(11);
        cell.setCellValue("特记事项");
        cell.setCellStyle(cellStyle);
        CellRangeAddress region5 =new CellRangeAddress(3, 4, 0, 0);
        //编号合并单元格
        sheet.addMergedRegion(region5);
        setCellBorder(sheet, region5);
        CellRangeAddress region6 =new CellRangeAddress(3, 4, 1, 1);
        //合并单元格
        sheet.addMergedRegion(region6);
        setCellBorder(sheet, region6);
        CellRangeAddress region7 =new CellRangeAddress(3, 4, 11, 11);
        //合并单元格
        sheet.addMergedRegion(region7);
        setCellBorder(sheet, region7);
        //3.构造sheet
        String[] titles = {"编号", "时间带", "设备故障","设备故障", "设备故障","设备故障","设备故障","设备故障","设备故障","设备故障","设备故障","特记事项"};
        Row row5 =sheet.createRow(4);
        AtomicInteger ai4 = new AtomicInteger();
        for (String title : titles) {
            cell = row5.createCell(ai4.getAndIncrement());
            cell.setCellValue(title);
            //设置样式
            cell.setCellStyle(cellStyle);
        }
        AtomicInteger datasAi = new AtomicInteger(5);
        int i = 1;
        Integer coatingProcess = 0;//中涂工序
        Integer paint1Process = 0;//色漆1工序
        Integer paint2Process = 0;//色漆2工序
        Integer varnishProcess = 0;//清漆工序
        Integer colorDifferenceProcess = 0;//色差工序
        Integer checkProcess = 0;//检查工序
        Integer hangProcess = 0;//上挂工序
        Integer blowProcess = 0;//吹气工序
        Integer dryingOven = 0;//干燥炉
        if(list.size()>0){
            for (EquipmentFailure report : list) {
                coatingProcess = coatingProcess+report.getCoatingProcess();
                paint1Process = paint1Process+report.getPaint1Process();
                paint2Process = paint2Process + report.getPaint2Process();
                varnishProcess = varnishProcess + report.getVarnishProcess();
                colorDifferenceProcess = colorDifferenceProcess+report.getColorDifferenceProcess();
                checkProcess = checkProcess+report.getCheckProcess();
                hangProcess = hangProcess + report.getHangProcess();
                blowProcess = blowProcess + report.getBlowProcess();
                dryingOven = dryingOven+report.getDryingOven();
                Row dataRow = sheet.createRow(datasAi.getAndIncrement());
                //编号
                cell = dataRow.createCell(0);
                cell.setCellValue(i++);
                cell.setCellStyle(cellStyle);
                //时间带
                cell = dataRow.createCell(1);
                cell.setCellValue(report.getTimeZone());
                cell.setCellStyle(cellStyle);
                //中涂工序
                cell = dataRow.createCell(2);
                cell.setCellValue(report.getCoatingProcess());
                cell.setCellStyle(cellStyle);
                //色漆1工序
                cell = dataRow.createCell(3);
                cell.setCellValue(report.getPaint1Process());
                cell.setCellStyle(cellStyle);
                //色漆2工序
                cell = dataRow.createCell(4);
                cell.setCellValue(report.getPaint2Process());
                cell.setCellStyle(cellStyle);
                //清漆工序
                cell = dataRow.createCell(5);
                cell.setCellValue(report.getVarnishProcess());
                cell.setCellStyle(cellStyle);
                //色差工序
                cell = dataRow.createCell(6);
                cell.setCellValue(report.getColorDifferenceProcess());
                cell.setCellStyle(cellStyle);
                //检查工序
                cell = dataRow.createCell(7);
                cell.setCellValue(report.getCheckProcess());
                cell.setCellStyle(cellStyle);
                //上挂工序
                cell = dataRow.createCell(8);
                cell.setCellValue(report.getHangProcess());
                cell.setCellStyle(cellStyle);
                //吹气工序
                cell = dataRow.createCell(9);
                cell.setCellValue(report.getBlowProcess());
                cell.setCellStyle(cellStyle);
                //干燥炉
                cell = dataRow.createCell(10);
                cell.setCellValue(report.getDryingOven());
                cell.setCellStyle(cellStyle);
                //特记事项
                cell = dataRow.createCell(11);
                cell.setCellValue(report.getRemark());
                cell.setCellStyle(cellStyle);
            }
            AtomicInteger ai6 = new AtomicInteger(list.size()+5);
            Row row6 = sheet.createRow(ai6.getAndIncrement());
            cell = row6.createCell(0);
            cell.setCellValue(" TOTAL");
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(1);
            cell.setCellValue(0);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(2);
            cell.setCellValue(coatingProcess);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(3);
            cell.setCellValue(paint1Process);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(4);
            cell.setCellValue(paint2Process);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(5);
            cell.setCellValue(varnishProcess);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(6);
            cell.setCellValue(colorDifferenceProcess);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(7);
            cell.setCellValue(checkProcess);
            cell.setCellStyle(cellStyle);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(8);
            cell.setCellValue(hangProcess);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(9);
            cell.setCellValue(blowProcess);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(10);
            cell.setCellValue(dryingOven);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(11);
            cell.setCellValue("");
            cell.setCellStyle(cellStyle);
            CellRangeAddress region8 =new CellRangeAddress(list.size()+5, list.size()+5, 0, 1);
            //合并单元格
            sheet.addMergedRegion(region8);
        }

        String fileName = URLEncoder.encode(sheet.getSheetName()+".xlsx", "UTF-8");
        response.setContentType("application/octet-stream");
        response.setHeader("content-disposition", "attachment;filename=" + new
                String(fileName.getBytes("ISO8859-1")));
        response.setHeader("filename", fileName);
        workbook.write(response.getOutputStream());
    }
    //合并单元格样式
    private void setCellBorder(Sheet sheet, CellRangeAddress cellAddresses) {
        RegionUtil.setBorderTop(BorderStyle.MEDIUM, cellAddresses, sheet);
        RegionUtil.setBorderBottom(BorderStyle.MEDIUM, cellAddresses, sheet);
        RegionUtil.setBorderLeft(BorderStyle.MEDIUM, cellAddresses, sheet);
        RegionUtil.setBorderRight(BorderStyle.MEDIUM, cellAddresses, sheet);


    }


}
